/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    TreeNode* pre = nullptr;
    int maxCount = 0;
    int count = 0;
    vector<int> rst;

    void traversal(TreeNode* root) {
        if(root == nullptr) return;

        traversal(root->left);

        if(pre == nullptr || pre->val != root->val) {
            count = 1;
            pre = root;
        } else {
            count = count + 1;
        }

        if(count == maxCount){
            rst.push_back(root->val);
        } else if(count > maxCount) {
            maxCount = count;
            rst.clear();
            rst.push_back(root->val);
        }

        traversal(root->right);
    }

    vector<int> findMode(TreeNode* root) {
        traversal(root);

        return rst;
    }
};